$(function () {
//地址库列表
    $.get(baseUrl+'/address/lists',{userId:userInfo.id},function(res){
        let data = res.data;
        html = '';
        data.forEach(function(item,index){
            if (item.isDefault == 1) {
                let html = `<p class="p1"><span>${item.consignee}</span> <span>${item.mobile}</span> </p>
                <p class="p2"><span>${item.provinceName}</span> <span>${item.cityName}</span>${item.areaName}<span></span> <span>${item.address}</span> </p>`
                $('.address_info').html(html)
            }
            console.log(item);
            html+=`	<li ${item.isDefault?"class='cur'":""} >
            <input type="radio" name="addressId" value="${item.id}" ${item.isDefault?"checked":""}>${item.consignee} ${item.provinceName} ${item.cityName} ${item.areaName} ${item.address} ${item.mobile} </li>`;
        })
        $('.address_select>ul').html(html)

    });
     //保存收货人信息
     $('.confirm_btn').click(function(){
         let userId = userInfo.id;
//  let consignee = $(this).parents('.address_select').siblings('.address_info').find('.p1').attr('consignee');
//  let mobile = $(this).parents('.address_select').siblings('.address_info').find('.p1').attr('mobile');
//  let province = $(this).parents('.address_select').siblings('.address_info').find('.p1').attr('province');
//  let city = $(this).parents('.address_select').siblings('.address_info').find('.p1').attr('city');
//  let area = $(this).parents('.address_select').siblings('.address_info').find('.p1').attr('area');
//  let address = $(this).parents('.address_select').siblings('.address_info').find('.p1').attr('address');
//  let isDefault = $(this).parents('.address_select').siblings('.address_info').find('.p1').attr('isDefault');
//  console.log(consignee);
        $.post(baseUrl+'/address/add',{ consignee,mobile, province,city, area,address,isDefault,userId},function(res){
            console.log(res);
        })
    })
    //点击显示
    $('.address>h3>a').click(function () {
        $('.address_select').show()
    })
    $('.new_address').click(function () {
        if ($(this).prop('checked')) {
            $('.none').show();
            //省市区3级联动
            $.get(baseUrl + '/system/province', { parentId: 1 }, function (res) {
                // console.log(res.data);
                let html = '';
                res.data.forEach(function (itme, index) {
                    // console.log(itme);
                    html += `<option value="${itme.id}">${itme.name}</option>`;
                });
                $('.sheng').html(html)
            });
            //市
            $('.sheng').on('change', function () {
                let id = ($(this).prop('value'));
                $.get(baseUrl + '/system/province', { parentId: id }, function (res) {
                    console.log(res.data);
                    let html = '';
                    res.data.forEach(function (itme) {
                        html += `<option value="${itme.id}">${itme.name}</option>`;
                    });
                    $('.shi').html(html)
                })
            });
            //区
            $('.shi').on('change', function () {
                let id = ($(this).prop('value'));
                // console.log(id);
                $.get(baseUrl + '/system/province', { parentId: id }, function (res) {
                    let html = '';
                    res.data.forEach(function (itme) {
                        html += `<option value="${itme.id}">${itme.name}</option>`;
                    });
                    $('.qu').html(html)
                })

            })
            //3级联动end
            //保存收货人信息
            
            // $('.confirm_btn').click(function () {
            //     let shname = $('.address_info>.p1').find('span').eq(0).text($('.shname').val());
            //     let phone = $('.address_info>.p1').find('span').eq(1).text($('.phone').val());

            //     let dz = $('.p2>span').eq(3).text($('.dz').val());
            //     let select1 = $('.sheng option:selected').text()
            //     let select2 = $('.shi option:selected').text()
            //     let select3 = $('.qu option:selected').text()
            //     //获取id值
            //     let shengId = $('.sheng option:selected').val()
            //     let shiId = $('.shi option:selected').val()
            //     let quid = $('.qu option:selected').val()
            //     // console.log(select1,select2,select3);
            //     $('.p2>span').eq(0).text(select1);
            //     $('.p2>span').eq(1).text(select2);
            //     $('.p2>span').eq(2).text(select3);
            //     let a = {
            //         consignee: shname,
            //         mobile: phone,
            //         province: shengId,
            //         city: shiId,
            //         area: quid,
            //         address: dz,
            //         isDefault: 1
            //     }
            //     console.log(a);
            //     $.post(baseUrl + '/address/add', a, function (res) {
            //         if (res.code != 0) {
            //             console.log(res.msg);
            //         }
            //     })
            //     return false;
            // })
          
        }
    })
    //购物车请求
    $.get(baseUrl + '/cart/lists', { userId:userInfo.id }, function (res) {

        console.log(res.data);
        let html = '';
        res.data.forEach(function (itme) {
            console.log(itme);
            html += `<tr>
        <td class="col1"><a href=""><img src="${itme.goods.goodsImg}" alt="" /></a>  <strong><a href="">${itme.goods.goodsName}</a></strong></td>
        <td class="col2"> <p>${itme.goods.goodsSn}</p> <p>${itme.goods.goodsBody}</p> </td>
        <td class="col3">￥<span>${itme.goods.shopPrice}</span></td>
        <td class="col4"> ${itme.goodsCount}</td>
        <td class="col5">￥<span>${itme.goods.shopPrice * itme.goodsCount}</span></td>
        </tr>`;
        })
        $('.cartList').html(html)
        let sum = 0;
        $('.cartList').find('.col5').find('span').each(function (itme) {
            sum += parseFloat($(this).text());
        })
        console.log(sum);
        $('.fillin_ft>p').find('span').text(sum.toFixed(2))
    })
    //跳转订单提交成功页面
    $('.fillin_ft input').click(function () {
        let data = $('.formaddress').serialize()
        data += '&userId='+userInfo.id
        //console.log(data);
        $.post(baseUrl+'/order/orderNow',data,function(res){
            console.log(res);
        })
        location.href = 'flow3.html';
        return false;
    })



});










